/**
 * //过去时间和现在时间的差 格式化时间
 * @param {String} time代表过去时间 ('2021-10-5 12:00:00')
 * @returns String
 */

function dateFormat(time) {
    let nowDate = new Date();
    let nowTime = nowDate.getTime();
    let oldDate = new Date(time);
    let oldTime = oldDate.getTime();
    let ChaS = (nowTime - oldTime) / 1000;
    // console.log(nowTime)
    // console.log(oldTime)
    // console.log(ChaS)

    if (ChaS < 10) return '刚刚'
    if (ChaS < 60) return `${ChaS}秒前`
    if (ChaS < 60 * 60) return `$(Math.floor(ChaS/60))分钟前`
    if (ChaS < 60 * 60 * 60) return `$(Math.floor(ChaS/60/60))小时前`
    if (ChaS < 60 * 60 * 60 * 24) return `$(Math.floor(ChaS/60/60/24))天前`
    if (ChaS < 60 * 60 * 60 * 24 * 30) return `$(Math.floor(ChaS/60/60/24/30))月前`
    if (ChaS < 60 * 60 * 60 * 24 * 30 * 12) return `$(Math.floor(ChaS/60/60/24/30/12))年前`
}

/**
 * 字符串补0  若是长度是一位 就在前面加0
 * @param {String/Number} date 
 * @returns 符合条件的一位数 
 */
function bu0(date) {
    return String(date).length == 1 ? 0 + 'date' : date;
}
//bu0(Chas)




/**
 * 返回随机时间
 * @returns String
 */
function randomTime() {
    let nian = Math.floor(Math.random() * (2022 - 0 + 1) + 0)
    let yue = Math.floor(Math.random() * (12 - 1 + 1) + 1)
    let ri = Math.floor(Math.random() * (31 - 1 + 1) + 1)
    let shi = Math.floor(Math.random() * (24 - 1 + 1) + 1)
    let fen = Math.floor(Math.random() * (60 - 1 + 1) + 1)
    let miao = Math.floor(Math.random() * (60 - 1 + 1) + 1)
    // console.log(`${nian}-${yue}-${ri} ${shi}:${fen}:${miao}`)
    return `${nian}-${yue}-${ri} ${shi}:${fen}:${miao}`

}





/**
  * 获取当前时间 返回年月日时分秒格式
  * @param {string} str 
  * @returns String
  */

function format(str) {
    var d = new Date()
    str = str.replaceAll('Y', d.getFullYear())
    str = str.replaceAll('m', bu0(d.getMonth() + 1))
    str = str.replaceAll('d', bu0(d.getDate()))
    str = str.replaceAll('H', bu0(d.getHours()))
    str = str.replaceAll('i', bu0(d.getMinutes()))
    str = str.replaceAll('s', bu0(d.getSeconds()))
    return str
}





/**
 * 获取当前浏览器类型
 * @returns String
 */
function browser() {
    if (navigator.userAgent.indexOf('Firebox') !== -1) {
        return '火狐浏览器'
    } else if (navigator.userAgent.indexOf('Chrome') !== -1) {
        return '谷歌浏览器'
    } else if (navigator.userAgent.indexOf('Safari') !== -1) {
        return '苹果浏览器'
    } else if (navigator.userAgent.indexOf('Opera') !== -1) {
        return 'Opera浏览器'
    } else if (navigator.userAgent.indexOf('Trident') !== -1) {
        return 'IE浏览器'
    }
}




/**
 * 判断是否是pc或m
 * @returns String
 */
function PcandYd() {
    data = navigator.userAgent
    if ((data.indexOf('Mac') || data.indexOf('Windows') || data.indexOf('Linux')) || data.indexOf('win') !== -1) {
        return '电脑端'
    }
    if ((data.indexOf('iPhone') || data.indexOf('iPad') || data.indexOf('Android') || data.indexOf('Mobile')) !== -1) {
        return '移动端'
    }
}



/**
 * 判断是否是微信浏览器
 * @returns String
 */
function weixin() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}




/**
 * 获取非行内样式
 * @param {String} obj 
 * @param {String} attr 
 * @returns String
 */
function getAttr(obj, attr) {
    let style;
    if (obj.currentStyle) {
        style = obj.currentStyle[attr]; //兼容IE 
    } else {
        style = getComputedStyle(obj, false)[attr]; //主流浏览器
        return style;
    }
}




/**
 * 
 * @param {string} key 键
 * @returns string
 */
function getParams(key) {
    let query = window.location.search.substring(1) //获得了当前链接的中?号后的参数
    query = decodeURI(query)
    let arr = query.split("&")
    for (let i = 0; i < arr.length; i++) {
        var tempArr = arr[i].split("=")
        if (tempArr[0] == key) return tempArr[1];
    }
    return false;
}
console.log(getParams(""))






/**
 * JS和JQ对象互相转换的原理
 * @param {string} ele 元素
 */
function jQueryFnInit(ele) {
    let eles = [];
    if (typeof ele === "object") {
        eles = [ele];
    } else {
        eles = document.querySelectorAll(ele);
    }
    [].push.apply(this, eles);
}

function $(ele) {
    return new jQueryFnInit(ele); 
}